    //
//  FocusListVC_iPad.m
//  WRY
//
//  Created by Yang on 11/8/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "FocusListVC_iPad.h"
#import "FocusDataMethod.h"

@implementation FocusListVC_iPad
@synthesize delegate;
/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	self.view.backgroundColor = [UIColor clearColor];
	_scrollView.backgroundColor = [UIColor clearColor];
	dataArray = [[NSMutableArray alloc] init];
	selectedBtTag = -1;
	[self reloadData];
    [super viewDidLoad];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(reloadData) 
												 name:@"FocusCompanyHasBeenChanged" 
											   object:nil];
}

- (void)reloadData {
	selectedBtTag = -1;
	[dataArray setArray:[[FocusDataMethod sharedInstance] data]];
	for (UIView *view in _scrollView.subviews) {
		[view removeFromSuperview];
	}
	for (int i = 0; i < [dataArray count]; i ++) {
		UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
		button.frame = CGRectMake(10.0f + 200 * (i % 5), 0.0f + 25 * (i / 5), 180.0f, 25.0f);
		[_scrollView addSubview:button];
		button.tag = 100 + i; // 从100开始记tag
		[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
		button.titleLabel.textAlignment = UITextAlignmentLeft;
		button.titleLabel.font = [UIFont boldSystemFontOfSize:14.0f];
		[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
		[button setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
		NSDictionary *dict = [dataArray objectAtIndex:i];
		[button setTitle:[dict objectForKey:@"EnterpriseName"] forState:UIControlStateNormal];
	}
	if ([dataArray count] / 5 > 3) {
		_scrollView.contentSize = CGSizeMake(1005.0f, 84 + ([dataArray count] / 5 - 2) * 25.0f );
	}
	if (selectedBtTag == -1 && [dataArray count]) {
		UIButton *button = (UIButton *)[_scrollView viewWithTag:100];
		[button sendActionsForControlEvents:UIControlEventTouchUpInside];
	}
	if ([dataArray count] == 0) {
		[delegate pressedOnFocusedCompany:nil companyType:nil];
	}
}
- (void)buttonPressed:(id)sender {
	UIButton *button = (UIButton *)sender;
	NSDictionary *dict = [dataArray objectAtIndex:button.tag - 100];
	//[delegate pressedOnFocusedCompany:[dict objectForKey:@"EnterpriseCode"]];
    [delegate pressedOnFocusedCompany:[dict objectForKey:@"EnterpriseCode"] companyType:[dict objectForKey:@"EnterpriseTypeCode"]];
	if (selectedBtTag != -1) {
		UIButton *bt = (UIButton *)[_scrollView viewWithTag:selectedBtTag];
		bt.selected = NO;
	}
	button.selected = YES;
	selectedBtTag = button.tag;
	//button.titleLabel.textColor = [UIColor orangeColor];
	//[delegate listPressedOnIndex:button.tag - 100];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Overriden to allow any orientation.
    return YES;
}


- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}


- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
	[dataArray release];
    [super dealloc];
}


@end
